package com.xjd.edu.toolkit.config;

import com.xjd.edu.toolkit.cache.GameRedisTemplate;
import com.xjd.edu.toolkit.helper.IdempotentHelper;
import com.xjd.edu.toolkit.lock.RedisLockExecutor;
import io.micrometer.core.instrument.MeterRegistry;
import lombok.extern.slf4j.Slf4j;
import org.redisson.api.RedissonClient;
import org.springframework.boot.autoconfigure.AutoConfigureOrder;
import org.springframework.boot.autoconfigure.condition.ConditionalOnBean;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.boot.web.client.RestTemplateBuilder;
import org.springframework.cloud.client.loadbalancer.LoadBalanced;
import org.springframework.context.annotation.Bean;
import org.springframework.core.Ordered;
import org.springframework.web.client.RestTemplate;

/**
 * 自动配置类
 *
 * @author wangyu
 * @created 2025/3/15 15:33
 */
@Slf4j
@AutoConfigureOrder(Ordered.LOWEST_PRECEDENCE)
public class ToolkitAutoConfiguration {


    @Bean
    @ConditionalOnBean(GameRedisTemplate.class)
    public IdempotentHelper idempotentHelper(GameRedisTemplate gameRedisTemplate) {
        return new IdempotentHelper(gameRedisTemplate);
    }

    @Bean
    @ConditionalOnBean(RedissonClient.class)
    public RedisLockExecutor redisLockExecutor(RedissonClient client, MeterRegistry registry) {
        final RedisLockExecutor helper = new RedisLockExecutor(client, registry);
        //LockUtil.init(helper);
        return helper;
    }


    @Bean
    @LoadBalanced
    @ConditionalOnMissingBean
    public RestTemplate restTemplate(RestTemplateBuilder builder) {
        return builder.build();
    }

}
